/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package nu.zoom.android.gdata.project.client;

import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public final class StandardMenuHandler {
	private final static String LOG_TAG = WKC.LOG_TAG;

	private StandardMenuHandler() {
		// Make it hard to instantiate this class, it isn't designed for it.
	}

	public static boolean onCreateOptionsMenu(final Activity activity,
			final Menu menu) {

		MenuInflater inflater = activity.getMenuInflater();
		inflater.inflate(R.menu.application, menu);
		return true;
	}

	public static boolean onOptionsItemSelected(final Activity activity,
			final MenuItem item) {

		switch (item.getItemId()) {
		case R.id.aboutMenuItem:
			Intent aboutIntent = new Intent(
					"org.openintents.action.SHOW_ABOUT_DIALOG");
			try {
				activity.startActivityForResult(aboutIntent, 0);
				return true;
			} catch (ActivityNotFoundException e) {
				Log.e(LOG_TAG,
						"OI About not installed, will try fallback dialog.");
				activity.showDialog(WKC.DialogID.ABOUT);
				return true;
			}
		case R.id.settingsMenuItem:
			Intent intent = Intents.settings(activity) ;
			activity.startActivity(intent);
		default:
			return false;
		}
	}

	public static Dialog onCreateDialog(final Activity activity, final int id) {
		switch (id) {
		case WKC.DialogID.ABOUT:
			return new FallbackAboutDialog(activity);
		default:
			return null;
		}
	}
}
